package game.Gamemode;

import javafx.scene.paint.Color;

public class ChatMessage {
    protected long sendTime;
    protected String senderName, message;
    protected Color senderColor;
    protected boolean global; // all teams can see

    public ChatMessage(String sender, Color senderColor, String msg, boolean global){
        this.sendTime = System.currentTimeMillis();

        this.senderName = sender;
        this.message = msg;
        this.senderColor = senderColor;
        this.global = global;

        setMessageWrap();
    }

    protected void setMessageWrap(){
        int i = 1;
        StringBuilder sb = new StringBuilder(message);
        int charWrap = 80;
        int currentSpaceFound = sb.indexOf(" ");
        while (currentSpaceFound != -1) {
            int lastOccurence = currentSpaceFound;
            currentSpaceFound = sb.indexOf(" ", currentSpaceFound + 1);
            if (currentSpaceFound > (charWrap * i)) {
                sb.setCharAt(lastOccurence, '\n');
                i++;
            }
        }
        message = sb.toString();
    }

    public String getSenderName(){
        return this.senderName;
    }

    public String getMessage(){
        return this.message;
    }

    public Color getSenderColor(){
        return this.senderColor;
    }

    public boolean isGlobal(){
        return this.global;
    }

     /**
     * Returns the time that message was sent at in milliseconds.
     * @return
     */
    public long getSendTime(){
        return this.sendTime;
    }

    /**
     * Returns the lifespan of the message in seconds.
     * @return Seconds that have passed since the message was sent.
     */
    public double getLifeSpan(){
        return (System.currentTimeMillis() - this.sendTime)/1000.0;
    }
}
